<?php

/*
 * @author Ebrain Cedeno <ebraincedeno@gmail.com>
 */

class VeCms{

   public static $app;
    
    public static function getPathRoot(){
        
        extract($_SERVER);
        $url_path = "http://".$HTTP_HOST.str_replace(basename($SCRIPT_NAME),"",$REQUEST_URI);
        if (file_exists($url_path)){
            return $url_path;
        }
        else{
            throw new Exception("no se encontro el directorio raiz");
        }
        
    }
    
    public static function getPathCss(){
        
        extract($_SERVER);
        
        if (stripos($REQUEST_URI,"?") !== false){
            $var_get = explode("?",$REQUEST_URI);
            $REQUEST_URI = $var_get[0];
        }
        
        $modules = substr(str_replace($SCRIPT_NAME,"",$PHP_SELF),1);
        if ($modules != ""){
            $url_param = explode("/",substr(str_replace($SCRIPT_NAME,"",$PHP_SELF),1));
            $url_path = "http://".$HTTP_HOST.str_replace(basename($SCRIPT_NAME),"",$REQUEST_URI)."base/css";
            $url_path = str_replace(implode($url_param,"/"),"", $url_path);
            
        }
        else{
            $url_path = "http://".$HTTP_HOST.str_replace(basename($SCRIPT_NAME),"",$REQUEST_URI)."base/css";
        }
        
        return $url_path;   
    }
    
    public static function getPathJs(){
        
        extract($_SERVER);
        
        if (stripos($REQUEST_URI,"?") !== false){
            $var_get = explode("?",$REQUEST_URI);
            $REQUEST_URI = $var_get[0];
        }
        
        $modules = substr(str_replace($SCRIPT_NAME,"",$PHP_SELF),1);
        
        if ($modules != ""){
            $url_param = explode("/",substr(str_replace($SCRIPT_NAME,"",$PHP_SELF),1));
            $url_path = "http://".$HTTP_HOST.str_replace(basename($SCRIPT_NAME),"",$REQUEST_URI)."base/javascript";
            $url_path = str_replace(implode($url_param,"/"),"", $url_path);
            
        }
        else{
            $url_path = "http://".$HTTP_HOST.str_replace(basename($SCRIPT_NAME),"",$REQUEST_URI)."base/javascript";
        }
        
        return $url_path;
    }
    
    public static function getPathImage(){
        
        extract($_SERVER);
        
        if (stripos($REQUEST_URI,"?") !== false){
            $var_get = explode("?",$REQUEST_URI);
            $REQUEST_URI = $var_get[0];
        }
        
        $modules = substr(str_replace($SCRIPT_NAME,"",$PHP_SELF),1);
        if ($modules != ""){
            $url_param = explode("/",substr(str_replace($SCRIPT_NAME,"",$PHP_SELF),1));
            $url_path = "http://".$HTTP_HOST.str_replace(basename($SCRIPT_NAME),"",$REQUEST_URI)."base/image";
            $url_path = str_replace(implode($url_param,"/"),"", $url_path);
            
        }
        else{
            $url_path = "http://".$HTTP_HOST.str_replace(basename($SCRIPT_NAME),"",$REQUEST_URI)."base/image";
        }
        
        return $url_path;
    }
    
    public static function getPathTemplete(){
        
        extract($_SERVER);
        
        if (stripos($REQUEST_URI,"?") !== false){
            $var_get = explode("?",$REQUEST_URI);
            $REQUEST_URI = $var_get[0];
        }
        
        $modules = substr(str_replace($SCRIPT_NAME,"",$PHP_SELF),1);
        if ($modules != ""){
            $url_param = explode("/",substr(str_replace($SCRIPT_NAME,"",$PHP_SELF),1));
            $url_path = "http://".$HTTP_HOST.str_replace(basename($SCRIPT_NAME),"",$REQUEST_URI)."theme";
            $url_path = str_replace(implode($url_param,"/"),"", $url_path);
            
        }
        else{
            $url_path = "http://".$HTTP_HOST.str_replace(basename($SCRIPT_NAME),"",$REQUEST_URI)."theme";
        }
        
        return $url_path;
        
    }
    
    public static function loadCssBase(){
        
        $script = '<link rel="stylesheet" type="text/css" href="'.self::getPathCss().'/css_base/main.css">'."\n";
        $script .= '<link rel="stylesheet" href="'.self::getPathCss().'/css_base/jquery.ui.all.css">'."\n";
        $script .= '<link rel="stylesheet" href="'.self::getPathCss().'/css_base/demos.css">'."\n";
        
        return $script;
    }
    
    public static function loadJsBase(){
        $script = '<script type="text/javascript" src="'.self::getPathJs().'/js_base/jquery-1.9.0.js"></script>'."\n";
        $script .= '<script type="text/javascript" src="'.self::getPathJs().'/js_base/jquery.ui.core.js"></script>'."\n";
        $script .= '<script type="text/javascript" src="'.self::getPathJs().'/js_base/jquery.ui.widget.js"></script>'."\n";
        $script .= '<script type="text/javascript" src="'.self::getPathJs().'/js_base/jquery.ui.datepicker.js"></script>'."\n";
        $script .= '<script type="text/javascript" src="'.self::getPathJs().'/js_base/jquery.ui.datepicker-es.js"></script>'."\n";
        $script .= '<script type="text/javascript" src="'.self::getPathJs().'/js_base/jquery.ui.mouse.js"></script>'."\n";
        $script .= '<script type="text/javascript" src="'.self::getPathJs().'/js_base/jquery.ui.draggable.js"></script>'."\n";
        $script .= '<script type="text/javascript" src="'.self::getPathJs().'/js_base/jquery.ui.position.js"></script>'."\n";
        $script .= '<script type="text/javascript" src="'.self::getPathJs().'/js_base/jquery.ui.resizable.js"></script>'."\n";
        $script .= '<script type="text/javascript" src="'.self::getPathJs().'/js_base/jquery.ui.button.js"></script>'."\n";
        $script .= '<script type="text/javascript" src="'.self::getPathJs().'/js_base/jquery.ui.dialog.js"></script>'."\n";
        return $script;
    }
    
    public static function createUrl($url,$param=array(),$optionsHtml=array()){
        
        extract($_SERVER);
        if (count($param) > 0){
            
            $getParameter = '?';
            foreach($param as $llave=>$value){
                $getParameter .= $llave."=".$value."&";
            }
        
            $getParameter = substr($getParameter, 0,-1);
        }
        else{
            $getParameter = "";
        }
        
        extract($_SERVER);
        if (stripos(strtolower($REQUEST_URI),"index.php")=== false){
            $url = str_replace("./","",$url);
            $modulo = array_keys(VeCms::$app->module_default);
            $url = 'index.php/'.$modulo[0]."/".$url;
        }
        elseif (str_replace("index.php", "", $REQUEST_URI) == "/" OR str_replace("index.php", "", $REQUEST_URI) == ""){
            $url = str_replace("./","",$url);
            $modulo = array_keys(VeCms::$app->module_default);
            $url = 'index.php/'.$modulo[0]."/".VeCms::$app->module_default[$modulo[0]];
        }
        
        return $url.$getParameter;
    }
    
    public static function AccessValidate($validate=array()){
        
        if (VeCms::$app->authentication == true){
        
            if (TSession::getStatusSession() == false){ //validamos que la session este iniciada si no lo esta la iniciamos
                $module = array_keys(VeCms::$app->module_default);
                header("location:".self::createUrl("../".$module[0]."/".VeCms::$app->module_default[$module[0]]));
                return false;
            }

            $access = false;
            foreach ($validate as $label=>$value){
                if (is_array($value)){
                    foreach($value as $llave=>$valor){
                        if ((int)TSession::getSession('user')->rol->$label == (int)$valor){
                            $access=true;
                        }
                    }

                }
                else{
                    if ((int)TSession::getSession('user')->rol->$label == (int)$value){
                       $access=true;
                    }
                }
            }

            if ($access == false){
                header("location:".self::createUrl("../error/access_not_allowed"));
            }
        }       
    }
    
}

class VeCmsEnum{
    CONST MODULE =0;
    CONST ACTION =1;
    
    //imagenes
    
    CONST ICON_VIEW = '/icons/view.png';
    CONST ICON_EDIT = '/icons/edit.png';
    CONST ICON_DELETE = '/icons/delete.png';
    CONST ICON_SAVE = '/icons/save.png';
    CONST ICON_UPDATE = '/icons/update.png';
    CONST ICON_ADD = '/icons/add.png';
    CONST ICON_SEARCH = '/icons/search.png';
    
}

?>
